package com.hr.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hr.model.DepartmentModel;
import com.hr.service.DepartmentService;

/**
 * 部门管理controller
 * @author fazhen.zheng
 *
 */
@Controller
public class DepartmentController {
	@Autowired
	private DepartmentService departmentService;
	
	/**
	 * 查询所有部门
	 * @return
	 */
	@RequestMapping("/department/list.html")
	public ModelAndView list(){
		List<DepartmentModel> departmentModels = departmentService.findAll();
		ModelAndView mv = new ModelAndView("/department/list");
		mv.addObject("departmentModels", departmentModels);
		return mv;
	}
	
	/**
	 * 新增或修改部门跳转
	 * @param request
	 * @param response
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/department/edit.html")
	public ModelAndView edit(String code) throws Exception {
		DepartmentModel department = null;
		if(code!=null)
			department = departmentService.findByPrimaryKey(code);
		ModelAndView mv = new ModelAndView("/department/edit");
		mv.addObject("department", department);
		return mv;
	}
	
	/**
	 * 增加或更新部门信息
	 * @param department
	 * @return
	 */
	@RequestMapping(value="/department/update.html")
	public ModelAndView update(DepartmentModel department){
		departmentService.editDepartment(department);
		return this.list();
	}
	/**
	 * 删除部门
	 * @param code
	 * @return
	 */
	@RequestMapping(value="/department/delete.html")
	public ModelAndView delete(String code){
		departmentService.removeDeparment(code);
		return this.list();
	}
}
